package com.starinside.wp.service.cookbook.impl;

import com.starinside.wp.domain.cookbook.RecipeConfig;
import com.starinside.wp.foundation.exception.BusinessException;
import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.repository.cookbook.RecipeConfigRepository;
import com.starinside.wp.service.cookbook.RecipeConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class RecipeConfigServiceImpl implements RecipeConfigService {

    @Autowired
    private RecipeConfigRepository configRepository;

    @Override
    public List<RecipeConfig> selectRecipeTypeByAreaId(String areaId) {
        return selectConfigList(areaId, "recipe_type");
    }

    @Override
    public List<RecipeConfig> selectFoodType(){
        return configRepository.findAllByAreaIdIsNullAndTypeAndDeletedIsFalseOrderBySortId("food_type");
    }

    @Override
    public List<RecipeConfig> selectConfigList(String areaId, String type) {
        return configRepository.findAllByAreaIdAndTypeAndDeletedIsFalseOrderBySortId(areaId, type);
    }

    @Override
    public RecipeConfig selectConfigNameIsExit(String areaId, String type, String recipeName) {
        return configRepository.findFirstByAreaIdAndTypeAndNameAndDeletedIsFalse(areaId, type, recipeName);
    }

    @Override
    public RecipeConfig insertRecipeType(String areaId, String recipeName) {

        List<RecipeConfig> configs = configRepository.findAllByAreaIdAndTypeAndDeletedIsFalseOrderBySortId(areaId, RECIPE_TYPE_KEY);

        int value = 0;
        if(configs.size() > 0) {
            value = configs.get(configs.size() - 1).getSortId() + 1;
        }

        RecipeConfig result = new RecipeConfig();

        result.setAreaId(areaId);
        result.setType(RECIPE_TYPE_KEY);
        result.setName(recipeName);
        result.setSortId(value);
        result.setValue(String.valueOf(value));
        result.setDeleted(false);

        configRepository.save(result);

        return result;
    }

    @Override
    public void updateConfigNameByAreaIdAndTypeAndValue(String name, String areaId, String type, String value) {
        configRepository.updateConfigNameBySchoolIDAndTypeAndValue(name, areaId, type, value);
    }

    @Override
    public void updateConfig(RecipeConfig config){
        configRepository.save(config);
    }

    @Override
    public RecipeConfig selectConfigByAreaIdAndTypeAndValue(String areaId, String type, String value) {
        return configRepository.findFirstByAreaIdAndTypeAndValueAndDeletedIsFalseOrderBySortId(areaId, type, value);
    }

    @Override
    public RecipeConfig deleteConfigByAreaIdAndTypeAndValue(String areaId, Long recipeId) {
        RecipeConfig config =  configRepository.findByIdAndAreaId(recipeId, areaId);
        if(config == null){
            throw new BusinessException(ErrorCode.RECIPE_NOT_EXIST, "餐次没找到");
        }
        config.setDeleted(true);
        configRepository.save(config);
        return config;
//        configRepository.deleteAllByAreaIdAndTypeAndValue(areaId, type, value);
    }

    @Override
    public void updateRecipeTypeSort(String areaId, String recipeId, int sort) {
        configRepository.updateRecipeTypeSort(areaId, recipeId, sort);
    }

    @Override
    public void upRecipeTypeSort(String areaId,String recipeId) {
        RecipeConfig targetRecipe = configRepository.findByIdAndAreaId(Long.valueOf(recipeId), areaId);
        RecipeConfig recipe = configRepository.findFirstByAreaIdAndTypeAndSortIdLessThanAndDeletedIsFalseOrderBySortIdDesc(areaId, "recipe_type", targetRecipe.getSortId());

        exchangeSortAndSave(targetRecipe, recipe);
    }

    @Override
    public void downRecipeTypeSort(String areaId, String recipeId) {
        RecipeConfig targetRecipe = configRepository.findByIdAndAreaId(Long.valueOf(recipeId), areaId);
        RecipeConfig recipe = configRepository.findFirstByAreaIdAndTypeAndSortIdGreaterThanAndDeletedIsFalseOrderBySortIdAsc(areaId, "recipe_type", targetRecipe.getSortId());

        exchangeSortAndSave(targetRecipe, recipe);
    }

    @Override
    public RecipeConfig selectRecipeConfigByAreaIdAndNameAndType(String areaId, String name, String type) {
        return configRepository.findByAreaIdAndNameAndType(areaId,name,type);
    }

    private void exchangeSortAndSave(RecipeConfig targetRecipe, RecipeConfig recipe){
        if(targetRecipe == null){
            throw new BusinessException(ErrorCode.RECIPE_NOT_EXIST, "餐次不存在");
        }

        if(recipe != null){//说明要排序的餐次不是第一个 还可以向上排序
            Integer temp = targetRecipe.getSortId();
            targetRecipe.setSortId(recipe.getSortId());
            recipe.setSortId(temp);
            List<RecipeConfig> objects = Arrays.asList(new RecipeConfig[]{recipe, targetRecipe});
            configRepository.saveAll(objects);
        }
    }
}
